// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraSettingValueDiscrete_OverallQuality.h"

#include "Engine/Engine.h"
#include "Settings/LyraSettingsLocal.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraSettingValueDiscrete_OverallQuality)

#define LOCTEXT_NAMESPACE "LyraSettings"

/**
 * 构造函数
 */
ULyraSettingValueDiscrete_OverallQuality::ULyraSettingValueDiscrete_OverallQuality()
{
}

/**
 * 初始化时调用
 */
void ULyraSettingValueDiscrete_OverallQuality::OnInitialized()
{
	Super::OnInitialized(); // 调用父类初始化方法

	ULyraSettingsLocal* UserSettings = ULyraSettingsLocal::Get(); // 获取用户设置
	const int32 MaxQualityLevel = UserSettings->GetMaxSupportedOverallQualityLevel(); // 获取最大支持的总体质量级别

	auto AddOptionIfPossible = [&](int Index, FText&& Value) { if ((MaxQualityLevel < 0) || (Index <= MaxQualityLevel)) { Options.Add(Value); }}; // 如果可能添加选项的Lambda

	AddOptionIfPossible(0, LOCTEXT("VideoQualityOverall_Low", "低")); // 添加低质量选项
	AddOptionIfPossible(1, LOCTEXT("VideoQualityOverall_Medium", "中")); // 添加中等质量选项
	AddOptionIfPossible(2, LOCTEXT("VideoQualityOverall_High", "高")); // 添加高质量选项
	AddOptionIfPossible(3, LOCTEXT("VideoQualityOverall_Epic", "史诗")); // 添加史诗质量选项

	OptionsWithCustom = Options; // 复制选项到包含自定义的选项
	OptionsWithCustom.Add(LOCTEXT("VideoQualityOverall_Custom", "自定义")); // 添加自定义选项

	const int32 LowestQualityWithFrameRateLimit = UserSettings->GetLowestQualityWithFrameRateLimit(); // 获取有帧率限制的最低质量
	if (Options.IsValidIndex(LowestQualityWithFrameRateLimit)) // 如果最低质量索引有效
	{
		SetWarningRichText(FText::Format(LOCTEXT("OverallQuality_Mobile_ImpactsFramerate", "<strong>注意：将质量设置更改为{0}或更高可能会限制您的帧率。</>"), Options[LowestQualityWithFrameRateLimit])); // 设置警告富文本
	}
}

/**
 * 存储初始设置
 */
void ULyraSettingValueDiscrete_OverallQuality::StoreInitial()
{
}

/**
 * 重置为默认值
 */
void ULyraSettingValueDiscrete_OverallQuality::ResetToDefault()
{
}

/**
 * 恢复到初始值
 */
void ULyraSettingValueDiscrete_OverallQuality::RestoreToInitial()
{
}

/**
 * 根据索引设置离散选项
 */
void ULyraSettingValueDiscrete_OverallQuality::SetDiscreteOptionByIndex(int32 Index)
{
	UGameUserSettings* UserSettings = CastChecked<UGameUserSettings>(GEngine->GetGameUserSettings()); // 获取游戏用户设置

	if (Index == GetCustomOptionIndex()) // 如果是自定义选项索引
	{
		// 保持原样，我们处于自定义设置中。
	}
	else
	{
		// 低 / 中 / 高 / 史诗
		UserSettings->SetOverallScalabilityLevel(Index); // 设置总体可伸缩性级别
	}

	NotifySettingChanged(EGameSettingChangeReason::Change); // 通知设置已更改
}

/**
 * 获取离散选项索引
 */
int32 ULyraSettingValueDiscrete_OverallQuality::GetDiscreteOptionIndex() const
{
	const int32 OverallQualityLevel = GetOverallQualityLevel(); // 获取整体质量级别
	if (OverallQualityLevel == INDEX_NONE) // 如果是无效索引
	{
		return GetCustomOptionIndex(); // 返回自定义选项索引
	}

	return OverallQualityLevel; // 返回整体质量级别
}

/**
 * 获取离散选项
 */
TArray<FText> ULyraSettingValueDiscrete_OverallQuality::GetDiscreteOptions() const
{
	const int32 OverallQualityLevel = GetOverallQualityLevel(); // 获取整体质量级别
	if (OverallQualityLevel == INDEX_NONE) // 如果是无效索引
	{
		return OptionsWithCustom; // 返回包含自定义的选项
	}
	else
	{
		return Options; // 返回选项
	}
}

/**
 * 获取自定义选项索引
 */
int32 ULyraSettingValueDiscrete_OverallQuality::GetCustomOptionIndex() const
{
	return OptionsWithCustom.Num() - 1; // 返回最后一个索引（自定义选项）
}

/**
 * 获取整体质量级别
 */
int32 ULyraSettingValueDiscrete_OverallQuality::GetOverallQualityLevel() const
{
	const UGameUserSettings* UserSettings = CastChecked<const UGameUserSettings>(GEngine->GetGameUserSettings()); // 获取游戏用户设置
	return UserSettings->GetOverallScalabilityLevel(); // 返回总体可伸缩性级别
}

#undef LOCTEXT_NAMESPACE